package com.openxt.nbzj.plugins;

import android.content.Context;
import android.content.DialogInterface;
import android.os.Handler;

import com.openxt.nbzj.activity.WebViewActivity;
import com.openxt.nbzj.util.App;
import com.openxt.nbzj.util.SystemUtil;

import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaPlugin;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import qs.openxt.libs.util.SecurityUtil;
import qs.openxt.libs.util.StringUtil;
import qs.openxt.libs.view.UIAlertView;

/**
 * Created by 颖杰_2 on 2016-08-27.
 */
public class MessageAppPlugin extends CordovaPlugin {
    private CallbackContext callbackContext = null;
    Handler mHandler = new Handler();
    Map<String, Boolean> map = new HashMap<>();

    public boolean execute(final String action, final JSONArray args, final CallbackContext callbackContext) throws JSONException {
        this.callbackContext = callbackContext;
        if ("doAlert".equals(action)) {
            final String message = args.getString(0);
            final String time = args.getString(1);
            final String uuid = UUID.randomUUID().toString();
            map.put(uuid, false);
            cordova.getActivity().runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    doAlert(message, time, uuid);
                }
            });
        } else if ("takeTitle".equals(action)) {
            final String title = args.getString(0);
            cordova.getActivity().runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    WebViewActivity viewActivity = (WebViewActivity) cordova.getActivity();
                    if (viewActivity == null)
                        return;
                    viewActivity.setTitle(title);
                }
            });
        }else if("md5Encrypt".equals(action)){
            long security_timestamp = SecurityUtil.getTimestamp();
            String token = SecurityUtil.getToken(App.APPKEY, App.SECRETKEY, security_timestamp);
            final JSONObject json = new JSONObject();
            json.put("time", String.valueOf(security_timestamp));
            json.put("token", token);
            mHandler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    callbackContext.success(json);
                }
            }, 32);
            return true;
        }else if ("shareSDK".equals(action)) {
            final String param = args.getString(0);
            cordova.getActivity().runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    WebViewActivity viewActivity = (WebViewActivity) cordova.getActivity();
                    if (viewActivity == null)
                        return;
                    viewActivity.showShareSDK(param);
                }
            });
        }else if ("showSearch".equals(action)) {
            cordova.getActivity().runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    WebViewActivity viewActivity = (WebViewActivity) cordova.getActivity();
                    if (viewActivity == null)
                        return;
                    viewActivity.showSearch();
                }
            });
        }else if("getDeviceMessage".equals(action)){
            final com.alibaba.fastjson.JSONObject device = SystemUtil.getDevice(cordova.getActivity());
            mHandler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    callbackContext.success(device.toString());
                }
            }, 32);
            return true;
        }else if("showTextNull".equals(action)){
            cordova.getActivity().runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    WebViewActivity viewActivity = (WebViewActivity) cordova.getActivity();
                    if (viewActivity == null)
                        return;
                    viewActivity.hideSearch();
                }
            });
        }

        return false;
    }

    private void doAlert(String message, String time, final String uuid) {
        try {
            Context mContext = cordova.getActivity();
            final UIAlertView dialog = new UIAlertView.Builder(mContext)
                    .setMessage(message)
                    .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();
                            map.put(uuid, true);
                        }
                    }).create();
            dialog.setCancelable(false);
            dialog.show();

            if (!StringUtil.isNull(time) && !"0".equals(time)) {
                mHandler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        if (map.get(uuid) == null || map.get(uuid))
                            return;
                        map.put(uuid, true);
                        cordova.getActivity().runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                dialog.dismiss();
                            }
                        });
                    }
                }, Integer.valueOf(time) * 1000);
            }
        }catch (Exception e){

        }
    }
}
